package com.shujia.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class Code02UpFile {
    public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {

        Configuration conf = new Configuration();

//        URI uri = new URI("hdfs://master:9000");
        // 通过连接获取其操作对象
//        FileSystem fileSystem = FileSystem.get(uri, conf);


        /**
         * The src file is on the local disk. Add it to FS at the given dst name. delSrc indicates if the source should be removed
         * Params:
         * delSrc – whether to delete the src 是否删除本地文件
         * src – path 本地文件路径
         * dst – path HDFS文件路径
         *
         * public void copyFromLocalFile(boolean delSrc, Path src, Path dst)
         */
//        fileSystem.copyFromLocalFile(false,new Path("data/aaa.txt"),new Path("/data/"));


        /**
         * 设置用户名方式：
         */

        //方式一：在当前系统变量中添加该配置  HADOOP_USER_NAME root


        /**
         * 方式二：
         *  public static FileSystem get(final URI uri, final Configuration conf,
         *         final String user)
         */
//        URI uri = new URI("hdfs://master:9000");
//        FileSystem fileSystem = FileSystem.get(uri, conf, "root");
//        fileSystem.copyFromLocalFile(false, new Path("data/aaa.txt"), new Path("/data/"));


        /**
         * 方式3:代码中修改系统配置
         */
        System.setProperty("HADOOP_USER_NAME","root");
        URI uri = new URI("hdfs://master:9000");
        FileSystem fileSystem = FileSystem.get(uri, conf);
        fileSystem.copyFromLocalFile(false, new Path("data/aaa.txt"), new Path("/data/bbb.txt"));


        // 关闭连接
        fileSystem.close();
    }
}
